package com.sgb.kjwl.viewmodel.manage

import android.content.Context
import androidx.lifecycle.MutableLiveData
import com.sgb.kjwl.api.APIManager
import com.swgk.core.base.api.ICallBack
import com.sgb.kjwl.api.service.WebsiteInfoAPI
import com.sgb.kjwl.model.entity.manage.CompanyAndDeptEntity
import com.sgb.link.entity.WebSiteAddressEntity
import com.sgb.kjwl.model.entity.user.AddressEntity
import com.swgk.core.base.model.entity.ListResponse
import com.swgk.core.base.BaseViewModel
import com.swgk.core.base.model.entity.BaseEntity
import com.swgk.core.dialog.DialogHelper

class WebsiteAddressModel  : BaseViewModel(){

    private var page = 1//页数
    private var limit = "10"//每页显示多少个
    private var currentCount = 10 //每页条数
    private var pages = 1 //总页数

    var mAddressList = MutableLiveData<ArrayList<WebSiteAddressEntity>>() //地址列表
    var mAddressDetailEntity = MutableLiveData<WebSiteAddressEntity>()
    var mAllAddress = MutableLiveData<ArrayList<AddressEntity<List<AddressEntity<List<AddressEntity<Any>>>>>>>()
    var mCompanys = MutableLiveData<List<CompanyAndDeptEntity>>()//公司企业列表

    /**
     * 地址列表查询
     * siteType 网站类型  0-个人 1-企业 2-团队
     *
     */
    fun getNewUserAddress(context : Context?, siteType : Int, isLoadMore: Boolean){
        if (isLoadMore) {
            if (currentCount < 10) {
                return
            }
        }
        if(!isLoadMore){
            pages = 1
        }
        page = if (isLoadMore) page + 1 else 1
        if (pages < page){
            return
        }
        DialogHelper.showProgressDialog(context!!, null, "数据加载中...", 0, false, null)
            .setCanceledOnTouchOutside(false)
        APIManager.getInstance().websiteInfoAPI.getNewUserAddress(WebsiteInfoAPI.UserCenterUrl.URL_ADDRESS_LIST, limit, page, siteType).enqueue(object : ICallBack<BaseEntity<ListResponse<List<WebSiteAddressEntity>>>>(){
            override fun onSuccess(data: BaseEntity<ListResponse<List<WebSiteAddressEntity>>>?) {
                if (data != null && data.data != null && data.data.list != null){
                    currentCount = data.data.list.size
                    pages = data.data.pageSize
                    mAddressList.postValue(data.data.list as ArrayList<WebSiteAddressEntity>)
                }
            }
        })
    }

    /**
     * 地址管理添加/修改
     */
    fun saveOrUpdateAddress(context : Context?, entity : WebSiteAddressEntity, isAdd : Boolean){
        APIManager.getInstance().websiteInfoAPI.saveOrUpdateAddress(entity).enqueue(object : ICallBack<BaseEntity<*>>(true, true){
            override fun onSuccess(data: BaseEntity<*>?) {
                message.postValue(if (isAdd) "提交成功" else "修改成功")
            }
        })
    }

    /**
     * 设置默认地址
     */
    fun setDefaultAddress(context : Context?, id : String){
        APIManager.getInstance().websiteInfoAPI.setDefaultAddress(id).enqueue(object : ICallBack<BaseEntity<*>>(true, true){
            override fun onSuccess(data: BaseEntity<*>?) {
                message.postValue("设置成功")
            }
        })
    }

    /**
     * 根据id获取地址详情
     */
    fun getDetailAddressById(context : Context?, id : String){
        APIManager.getInstance().websiteInfoAPI.getDetailAddressById(id).enqueue(object : ICallBack<BaseEntity<WebSiteAddressEntity>>(true, true){
            override fun onSuccess(data: BaseEntity<WebSiteAddressEntity>?) {
                if (data != null && data.data != null){
                    mAddressDetailEntity.postValue(data.data)
                }
            }
        })
    }

    /**
     * 个人----根据ids批量删除
     */
    fun bathDel(context : Context?, id : String){
        APIManager.getInstance().websiteInfoAPI.bathDel(arrayOf(id)).enqueue(object : ICallBack<BaseEntity<*>>(true, true){
            override fun onSuccess(data: BaseEntity<*>?) {
                message.postValue("删除成功")
            }
        })
    }

    /**
     * 企业----根据id删除
     */
    fun companyAddressDel(context : Context?, id : String){
        APIManager.getInstance().websiteInfoAPI.companyAddressDel(id).enqueue(object : ICallBack<BaseEntity<*>>(true, true){
            override fun onSuccess(data: BaseEntity<*>?) {
                message.postValue("删除成功")
            }
        })
    }


    /**
     * 所有地址选项
     */
    fun getAddressModel(context: Context?) {
        APIManager.getInstance().goodsAPI.cityList.enqueue(object : ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity<Any>>>>>>>>(true, true){
            override fun onSuccess(data: BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity<Any>>>>>>>?) {
               if (data != null && data.data != null){
                   mAllAddress.postValue(data.data as ArrayList<AddressEntity<List<AddressEntity<List<AddressEntity<Any>>>>>>)
               }
            }

         })
    }

    //获取企业下一二级公司
     fun getCompanyList() {
        APIManager.getInstance().websiteInfoAPI.addressCompanys.enqueue(object : ICallBack<BaseEntity<BaseEntity<List<CompanyAndDeptEntity>>>>(true, true){
            override fun onSuccess(data: BaseEntity<BaseEntity<List<CompanyAndDeptEntity>>>?) {
                if (data != null && data.data != null && data.data.data != null){
                    mCompanys.postValue(data.data.data)
                }
            }
        })
    }

}